package rongqi;

import java.util.*;

public class RandomNumberOperations {

    public static void main(String[] args) {
        // 1. 用List存放10个随机数，下标遍历打印，foreach遍历打印
        List<Integer> list = new ArrayList<>();
        Random rand = new Random();
        for (int i = 0; i < 10; i++) {
            list.add(rand.nextInt(100)); // 随机数范围0-99
        }

        System.out.println("Index-based iteration:");
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        System.out.println("Foreach iteration:");
        for (int num : list) {
            System.out.println(num);
        }

        // 2. 用自己定义的Stack放入10个随机数，弹出5个，再压入5个随机数。打印当前存放的所有数据。
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 10; i++) {
            stack.push(rand.nextInt(100));
        }

        for (int i = 0; i < 5; i++) {
            stack.pop();
        }

        for (int i = 0; i < 5; i++) {
            stack.push(rand.nextInt(100));
        }

        System.out.println("Current stack contents:");
        for (int num : stack) {
            System.out.println(num);
        }

        // 3. 用Queue放入10个随机数，取出5个，再放入5个。打印当前存放的所有数据。
        Queue<Integer> queue = new LinkedList<>();
        for (int i = 0; i < 10; i++) {
            queue.offer(rand.nextInt(100));
        }

        for (int i = 0; i < 5; i++) {
            queue.poll();
        }

        for (int i = 0; i < 5; i++) {
            queue.offer(rand.nextInt(100));
        }

        System.out.println("Current queue contents:");
        for (int num : queue) {
            System.out.println(num);
        }

        // 4. 用TreeSet放入100个10-20之间的随机数。打印当前存放的所有数据。
        TreeSet<Integer> treeSet = new TreeSet<>();
        for (int i = 0; i < 100; i++) {
            treeSet.add(10 + rand.nextInt(11)); // 10-20之间的随机数
        }

        System.out.println("TreeSet contents:");
        for (int num : treeSet) {
            System.out.println(num);
        }

        // 5. 用TreeMap放入10000个0-99之间的随机数。统计并打印每个数出现的频率。
        TreeMap<Integer, Integer> treeMap = new TreeMap<>();
        for (int i = 0; i < 10000; i++) {
            int number = rand.nextInt(100);
            treeMap.put(number, treeMap.getOrDefault(number, 0) + 1);
        }

        System.out.println("Number frequencies in TreeMap:");
        for (Map.Entry<Integer, Integer> entry : treeMap.entrySet()) {
            System.out.println("Number: " + entry.getKey() + " Frequency: " + entry.getValue());
        }
    }
}
